<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Loader;

class Login extends Controller
{

    public function index()
    {
        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));exit;
            $username = trim(input('post.username'));
            $password = trim(input('post.pwd'));
            $verify = trim(input('post.verify'));

            if (!$username || !$password || !$verify) {
                $this->error('用户名/密码/验证码不能为空');
            }

            if (!captcha_check($verify)) {
                $this->error('验证码不正确');
            }

            $info = db('adminuser')->field('id,admin_name,password,role_id')->where('admin_name', $username)->find();

            if (!$info) {
                $this->error('用户不存在');
            }

            if (md5($password) != $info['password']) {
                $this->error('密码不正确');
            }

            $time = time();
            $ip = Request::instance()->ip();

            $result = \think\Db::execute('update dean_adminuser set ltime = '. "$time".', loginip = inet_aton("'.$ip.'")  where id = '.$info['id']);
            // dump($result);exit;
            if ($result===false) {
                $this->error('登陆失败,请稍后重试');
            }
        //记录登陆信息
            $browser = browse_info();
            @\think\Db::execute("insert into  dean_login_note(`userid`,`lgtime`,`lgip`,`browser`) values('".$info['id']."', '".$time."', inet_aton('".$ip."'), '".$browser."')");
            
            session('user_name', $info['admin_name']);
            session('user_id', $info['id']);
            session('user_role_id', $info['role_id']);

            $this->success('登入成功', 'index/index');
        } else {
            if (session('?user_name')) {
                $this->success('您已登入', 'index/index');
            } else {
                return  $this->fetch();
            }
        }
    }

    /**
    * 登出
    * 1,直接调用方法跳转:修改密码后
    * 2,ajax请求:退出按钮
    */
    public function logout()
    {
        session(null);
        if (Request::instance()->isAjax()) {
            session(null);
            return  ['status'=>0,'msg'=>'退出成功'];
        } else {
            session(null);
            return $this->fetch('index');
        }
    }
    
    /*
     * getPwd 找回密码
     */
    public function getPwd()
    {
        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));
            // exit;
            $username = input('post.username');
            $password = md5(input('post.password'));
            $email_code = input('post.email_code');
            $repassword = md5(input('post.repassword'));
            
            if (!$username || !$email_code || !$password || $password != $repassword) {
                $this->error('密码重置失败,请重试');
            }

            if ($username !=session('username') || $email_code !=session('token')) {
                $this->error('密码重置失败,请重试');
            }

            $result = \think\Db::execute('update dean_adminuser set password = "'.$password.'"  where admin_name= "'.$username.'";');

            if ($result) {
                session(null);
                $this->success('密码重置成功,请重新登录', 'index');
            } else {
                $this->error('密码重置失败,请重试');
            }
        }
        return $this->fetch();
    }


    public function sendMailCode()
    {
        $username = input("post.username");
        $email = input("post.email");
        
        if (empty($username) || empty($email)) {
            return ['status'=>0,'msg'=>'* 用户名/邮箱不存在'];
        }
        
        $info =  db('adminuser')->field('id,admin_name,email')->where('admin_name', $username)->find();
        
        if (!$info) {
            return ['status'=>0,'msg'=>'* 用户不存在'];
        }

        if ($email != $info['email']) {
            return ['status'=>1,'msg'=>'* 邮箱不正确'];
        }
        
        $token = mt_rand(100000, 999999); //创建用于找回密码的验证码
    
        $emailtype = "HTML";
        $smtpemailto = $email;
        $smtpemailfrom = "huwhereis@163.com";
        $emailsubject = "用户帐号激活";
        $emailbody = "亲爱的".$username."：<br/>您正在使用邮箱找回密码功能,下面是此次的验证码:<br/>";
        $emailbody .="<b>".$token."</b><br/>30分钟内有效<p style='text-align:right'>-------- huwhois敬上</p>";

        $smtp = new \mylib\Smtp();

        $rs = $smtp->sendmail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
        // $rs = 1;
        // var_dump($rs);
        // exit;
        if ($rs==1) {
            session('token', $token);
            session('username', $username);
            return ['status'=>1,'msg'=>'验证码发送成功, 请注意查收'];
        } else {
            return ['status'=>0,'msg'=>'验证码发送失败,请稍后重试'];
        }
    }

    public function checkEmailCode()
    {
        if (session('?token')) {
            $token =intval(input("post.email_code")) ;
            if ($token==session('token')) {
                return ['status'=>1,'msg'=>'验证码OK'];
            } else {
                return ['status'=>1,'msg'=>'验证码错误, 请稍后重试'];
            }
        } else {
            return ['status'=>0,'msg'=>'验证码失效, 请稍后重试'];
        }
    }
}
